import React from 'react';
import { inject, observer } from 'mobx-react';
import { Button, Row, Col, Modal } from 'antd';
import { WeaDialog, WeaSearchGroup, WeaFormItem, WeaInput, WeaBrowser, WeaSelect, WeaError } from 'ecCom';
import { toJS } from 'mobx';
import equal from 'deep-equal';
import { WeaLocaleProvider } from 'ecCom';

const getLabel = WeaLocaleProvider.getLabel;

@observer
class SearchAssignDialog extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      replaceDatas: [],
      ids: '',
    };
  }
  componentWillReceiveProps(nextProps) {

  }
  shouldComponentUpdate() {
    return true;
  }
  render() {
    const { baseParams, showCustomerModal, showModal, assignCrmName } = this.props.contentStore;
    const formItemLayout = {
      labelCol: { span: 6 },
      wrapperCol: { span: 14 },
    };
    const { replaceDatas } = this.state;
    return (
      <WeaDialog
        ecId={`${this && this.props && this.props.ecId || ''}_WeaDialog@f2k3tu`}
        title={baseParams.selectType == 'assign' ? getLabel(126086, '分配客户经理') : getLabel(103, '修改') + getLabel(144, '经理')}
        icon="icon-coms-crm"
        iconBgcolor="#96358a"
        visible={showModal}
        style={{ width: 520, height: 200 }}
        buttons={[
          <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@f9wnjn@save`} type="primary" onClick={() => this.doSave(baseParams.selectType)} >{getLabel(86, '保存')}</Button>,
          <Button ecId={`${this && this.props && this.props.ecId || ''}_Button@0eb1wt@close`} onClick={() => showCustomerModal(false)}>{getLabel(309, '关闭')}</Button>,
        ]}
        onCancel={() => showCustomerModal(false)}
        hasScroll
      >
        <WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@n2pw3j`} needTigger title={getLabel(1361, '基本信息')} showGroup >
          {
            baseParams.selectType == 'assign' &&
            <div>
              <WeaFormItem
                ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@twuw4c`}
                label={getLabel(1268, '客户名称')}
                {...formItemLayout}
              >
                <WeaInput ecId={`${this && this.props && this.props.ecId || ''}_WeaInput@5ae33e`} viewAttr={1} value={assignCrmName} />
              </WeaFormItem>

              <WeaFormItem
                ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@s1cbh5`}
                label={getLabel(1278, '客户经理')}
                {...formItemLayout}
              >

                <WeaError
                  ecId={`${this && this.props && this.props.ecId || ''}_WeaError@4pa4cg`}
                  tipPosition="top"
                  ref="weaError"
                  error={`"${getLabel(1278, '客户经理')}"${getLabel(21423, '未填写')}`}
                >
                  <WeaBrowser
                    ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@6pifws`}
                    replaceDatas={replaceDatas}
                    inputStyle={{ width: 200 }}
                    type={1}
                    viewAttr={3}
                    title={getLabel(1867, '人员')}
                    onChange={(ids, names, datas) => { this.browserChange(ids, names, datas); }}
                  />
                </WeaError>

              </WeaFormItem>

            </div>
          }
          {
            baseParams.selectType == 'monitor' &&
            <div>
              <WeaFormItem
                ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@tpqcxc`}
                label={getLabel(21956, '对象类型')}
                {...formItemLayout}
              >
                <WeaSelect ecId={`${this && this.props && this.props.ecId || ''}_WeaSelect@irkl9z`} style={{ width: '150px' }} viewAttr={2} value={`${1}`} options={[{ key: '1', selected: true, showname: getLabel(179, '人力资源') }]} />
              </WeaFormItem>
              <WeaFormItem
                ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@1dc6w3`}
                label={getLabel(106, '对象')}
                {...formItemLayout}
              >
                <WeaError
                  ecId={`${this && this.props && this.props.ecId || ''}_WeaError@arskb3`}
                  tipPosition="top"
                  ref="weaError"
                  error={`"${getLabel(106, '对象')}"${getLabel(21423, '未填写')}`}
                >
                  <WeaBrowser
                    ecId={`${this && this.props && this.props.ecId || ''}_WeaBrowser@08k2gl`}
                    replaceDatas={replaceDatas}
                    inputStyle={{ width: 200 }}
                    type={1}
                    viewAttr={3}
                    title={getLabel(1867, '人员')}
                    onChange={(ids, names, datas) => { this.browserChange(ids, names, datas); }}
                  />
                </WeaError>
              </WeaFormItem>
            </div>
          }
        </WeaSearchGroup>
      </WeaDialog>
    );
  }

        browserChange =(ids, names, datas) => {
          this.setState({
            replaceDatas: datas,
            ids,
          });
        }

        doSave=(type) => {
          const { customerIds, saveCustomerAssign, saveChangeManager, showCustomerModal } = this.props.contentStore;
          const { ids } = this.state;
          if (ids) {
            { type == 'assign' && saveCustomerAssign({ customerIds, manager: ids }); }
            { type == 'monitor' && saveChangeManager({ customerIds, relatedShareId: ids }); }
            showCustomerModal(false); // 关闭
            this.setState({
              replaceDatas: [],
              ids: '',
            });
          } else {
            this.refs.weaError.showError();
            // Modal.warning({
            //    title: getLabel(15172,"系统提示"),
            //    content: getLabel(129512,"必填字段不能为空！"),
            //    okText:getLabel(33703,"确定")
            // });
          }
        }
}

export default SearchAssignDialog;
